package com.nmgydy.util;

import okhttp3.OkHttpClient;

import javax.net.ssl.*;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.concurrent.TimeUnit;

public class OkHttpUtil {

    private static final OkHttpClient okHttpClient;

    static {
        SSLSocketFactory ssfFactory = null;
        SSLContext sc = null;
        try {
            sc = SSLContext.getInstance("TLS");
            sc.init(null, new TrustManager[]{new TrustAllCerts()}, new SecureRandom());
        } catch (Exception e) {
            e.printStackTrace();
        }
        ssfFactory = sc.getSocketFactory();
        okHttpClient = new OkHttpClient().newBuilder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60,TimeUnit.SECONDS)
                .writeTimeout(60,TimeUnit.SECONDS)
                .sslSocketFactory(ssfFactory,new TrustAllCerts())
                .hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String s, SSLSession sslSession) {
                        return true;
                    }
                })
                .build();
    }

    public static OkHttpClient getInstance(){
        return okHttpClient;
    }

}
